package com.sbio.udiapp.ui;

import static com.sbio.udiapp.util.LogTag.getTag;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.UiThread;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.sbio.udiapp.databinding.ActivityLogBinding;
import com.sbio.udiapp.net.api.ApiService;
import com.sbio.udiapp.util.DefaultSharedPreferencesUtil;
import com.sbio.udiapp.util.FileUtil;
import com.sbio.udiapp.util.LogUtil;
import com.sbio.udiapp.util.ZipUtil;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class LogActivity extends AppCompatActivity {

    @Inject
    ApiService apiService;

    ActivityLogBinding binding;

    @Override
    @UiThread
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLogBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.btnUpload30Log.setOnClickListener(v -> {
            String deviceNo = DefaultSharedPreferencesUtil.getDeviceNo(this);
            LogUtil.file("上传日志","上传30天日志");
            long mills = LocalDate.now().plusDays(-30).atTime(LocalTime.MIN).toInstant(ZoneOffset.of("+8")).toEpochMilli();
            List<File> logFiles = LogUtils.getLogFiles();
            logFiles = logFiles.stream().filter(file -> file.lastModified() > mills).collect(Collectors.toList());
            if (logFiles.size() > 1) {
                CompressTask compressTask = new CompressTask(apiService,
                        logFiles, this.getCacheDir() + "/" + System.currentTimeMillis() + ".zip");
                compressTask.execute();
            } else {
                uploadLog(logFiles.get(0));
            }
        });

        binding.btnUploadCurrentLog.setOnClickListener(v -> {
            String deviceNo = DefaultSharedPreferencesUtil.getDeviceNo(this);
            LogUtil.file("上传日志","上传当前日志");
            File file = new File(LogUtils.getCurrentLogFilePath());
            uploadLog(file);
        });

    }

    private void uploadLog(File file) {

        FileUtil.uploadLog(apiService, file);
    }


    private static class CompressTask extends AsyncTask<Void, Void, File> {
        private final List<File> files;
        private final String zipFileName;

        private final ApiService apiService;

        public CompressTask(ApiService apiService, List<File> files, String zipFileName) {
            this.apiService = apiService;
            this.files = files;
            this.zipFileName = zipFileName;
        }

        @Override
        protected File doInBackground(Void... voids) {
            try {
                return ZipUtil.compressLogsToZip(files, zipFileName);
            } catch (IOException e) {
                return null;
            }
        }

        @Override
        protected void onPostExecute(File file) {
            FileUtil.uploadLog(apiService, file);
        }


    }
}



